package com.woke.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.HtmlActivity;
import com.example.woke.PeaceActivity;
import com.http.HttpMethods;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.woke.adapter.ExChangeViewProvider;
import com.woke.adapter.OnItemClickListener;
import com.woke.addressactivity.ExChangeActivity;
import com.woke.data.UserPromoteCode;
import com.woke.method.MyApp;
import com.woke.method.NetworkUtils;
import com.wokeMy.view.LotteryMainActivity;
import com.zhongcai.online.R;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 中交所 列表
 */
public class ExchangeFragment extends Fragment implements XRecyclerView.LoadingListener {

	private static final String TAG = ExchangeFragment.class.getSimpleName();
	private View rootView;
	private XRecyclerView mRecyclerView;
	private RelativeLayout rlheead;
	private List<UserPromoteCode> mExchangeList = new ArrayList<>();
	public static final int TYPE_LOAN = 1; // 贷款
	public static final int TYPE_CREDIT = 2;// 信用卡
	private int mType;
	private MultiTypeAdapter mMulitypeAdapter;
	private Items mItems;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		if (bundle != null) {
			mType = bundle.getInt("type");
			Log.e("type",""+mType);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		if (rootView == null) {
			rootView = inflater
					.inflate(R.layout.promote_list, container, false);
			View headerView = inflater.inflate(R.layout.exchange_header, container, false);

			mRecyclerView = (XRecyclerView) rootView
					.findViewById(R.id.promoteCodeList_recyclerView);

			rlheead=(RelativeLayout) rootView.findViewById(R.id.promote_view_rl);
			rlheead.setOnClickListener(new View.OnClickListener() {
				 @Override
				 public void onClick(View v) {

					 if (mType==3){
						 MyApp application = (MyApp) getActivity().getApplication();
						 int userid = Integer.parseInt(application.getDatas_load().getId());
						 HttpMethods.getInstance().fetchDirectUrl(carInsuranceObserver, userid);

					 }
					 else if(mType==4){
						 Intent intent =new Intent(getActivity(), LotteryMainActivity.class);
						 startActivity(intent);
					 }
				 }
			 });
			ImageView view=(ImageView) rootView.findViewById(R.id.promote_view);
			TextView tvBt=(TextView) rootView.findViewById(R.id.promote_view_bt);
			mRecyclerView.addHeaderView(headerView);
			mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
			mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
			mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
			mRecyclerView.setLoadingListener(this);
			initMulitypeAdapter();

			if (mType==3){
				rlheead.setVisibility(View.VISIBLE);
				view.setBackgroundResource(R.mipmap.chexian);
			} else if(mType==4){
				view.setBackgroundResource(R.mipmap.caipiao);
				tvBt.setVisibility(View.GONE);
				rlheead.setVisibility(View.VISIBLE);
			}else {
				headerView.setBackgroundResource(R.mipmap.zj_banner);
				getpRromoteFromHttp();
			}


		} else {
			ViewGroup parent = (ViewGroup) rootView.getParent();
			if (parent != null) {
				parent.removeView(rootView);
			}
		}
		return rootView;
	}

	private void initMulitypeAdapter() {

		mItems = new Items();
		mMulitypeAdapter = new MultiTypeAdapter();
		mMulitypeAdapter.register(UserPromoteCode.class, new ExChangeViewProvider(mItemClickListener));
		mRecyclerView.setAdapter(mMulitypeAdapter);
		mRecyclerView.setLoadingMoreEnabled(false);
		mMulitypeAdapter.setItems(mItems);
		mMulitypeAdapter.notifyDataSetChanged();
//		if (mItems.size () > 0) {
//			assertAllRegistered (mMulitypeAdapter, mItems);//执行这句话时,必须保证 Items.size()>0
//		}

	}

	private OnItemClickListener mItemClickListener = new OnItemClickListener() {
		@Override
		public void OnItemClick(View view, int position) {

		}

		@Override
		public void OnItemClick(View view, Object object, int position) {

			if (object instanceof UserPromoteCode) {
				UserPromoteCode code = (UserPromoteCode) object;
				Intent intent = new Intent(getActivity(), PeaceActivity.class);
				intent.putExtra("url", code.getLink());
				intent.putExtra("title", code.getName());
				startActivity(intent);
			}
		}
	};


	public void getpRromoteFromHttp() {
		HttpMethods.getInstance().exchangeOrgList(mOrgListObserver, mType);
	}

	private Observer<List<UserPromoteCode>> mOrgListObserver = new Observer<List<UserPromoteCode>>() {
		@Override
		public void onSubscribe(Disposable d) {

		}

		@Override
		public void onNext(List<UserPromoteCode> value) {
			if (value != null && value.size() > 0) {
				mItems.clear();
				mItems.addAll(value);
				mItems.addAll(mExchangeList);
				if (mMulitypeAdapter != null) {
					mMulitypeAdapter.notifyDataSetChanged();
				}
			}
		}

		@Override
		public void onError(Throwable e) {
			if (mRecyclerView != null) {
				mRecyclerView.refreshComplete();
				mRecyclerView.loadMoreComplete();
			}
		}

		@Override
		public void onComplete() {
			if (mRecyclerView != null) {
				mRecyclerView.refreshComplete();
				mRecyclerView.loadMoreComplete();
			}
		}
	};

	@Override
	public void onRefresh() {
		if (getContext() != null) {
			if (NetworkUtils.isConnected(getActivity())) {
				getpRromoteFromHttp();
			} else {
				mRecyclerView.refreshComplete();
			}
		}

	}

	@Override
	public void onLoadMore() {
		mRecyclerView.loadMoreComplete();
	}
	private Observer<String> carInsuranceObserver = new Observer<String>() {
		@Override
		public void onSubscribe(Disposable d) {

		}

		@Override
		public void onNext(String value) {
			if (!TextUtils.isEmpty(value)) {
				if (getContext() != null) {
					Intent intent = new Intent(getContext(), HtmlActivity.class);
					intent.putExtra("urlname", "众安车险");
					intent.putExtra("url", value);
					getContext().startActivity(intent);

				}
			}
		}

		@Override
		public void onError(Throwable e) {
			if (e != null) {
				Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		public void onComplete() {

		}
	};
}
